﻿Public Class UCTimKiem
    Dim connect As New connect()
    Public Sub ganTT(ByVal ID As String, ByVal tenKH As String, ByVal CMND As String)
        Me.txtMaKH.Text = ID
        Me.txtTenKH.Text = tenKH
        Me.txtCMND.Text = CMND
    End Sub

    Private Sub UCTimKiem_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        LoadcmbLoai()
        LoadcmbQuan()
        LoadcmbGia()
        LoadcmbDienTich()
        LoadcmbSucChua()
        LamMoi()
    End Sub
    Private Sub LoadcmbLoai()
        Dim ds As New DataSet("GIATIEN")
        ds = connect.LoadLoaiPhong("select MALP,TENLP from LOAIPHONG")
        Dim newrow As DataRow = ds.Tables("LOAIPHONG").NewRow()
        newrow("TENLP") = "Tất cả"
        newrow("MALP") = 0
        ds.Tables("LOAIPHONG").Rows.Add(newrow)
        With cmbLoai
            .DataSource = ds.Tables("LOAIPHONG")
            .DisplayMember = "TENLP"
            .ValueMember = "MALP"
        End With
        cmbLoai.SelectedValue = 0
    End Sub
    Private Sub LoadcmbGia()
        Dim dt As DataTable = New DataTable("GIATIEN")
        dt.Columns.Add("ID")
        dt.Columns.Add("GIA")

        dt.Rows.Add(1, "Nhỏ hơn 1 triệu")
        dt.Rows.Add(2, "1 triệu - 2 triệu")
        dt.Rows.Add(3, "2 triệu - 5 triệu")
        dt.Rows.Add(4, "Lớn hơn 5 triệu")
        dt.Rows.Add(0, "Tất cả")
        Dim ds As New DataSet("GIATIEN")
        ds.Tables.Add(dt)
        With cmbGia
            .DataSource = ds.Tables("GIATIEN")
            .DisplayMember = "GIA"
            .ValueMember = "ID"
        End With
        cmbGia.SelectedValue = 0
    End Sub
    Private Sub LoadcmbDienTich()
        Dim dt As DataTable = New DataTable("DIENTICH")
        dt.Columns.Add("ID")
        dt.Columns.Add("M2")

        dt.Rows.Add(1, "Nhỏ hơn 20m2")
        dt.Rows.Add(2, "20m2 - 30m2")
        dt.Rows.Add(3, "30m2 - 50m2")
        dt.Rows.Add(4, "Lớn hơn 50m2")
        dt.Rows.Add(0, "Tất cả")
        Dim ds As New DataSet("DIENTICH")
        ds.Tables.Add(dt)
        With cmbDienTich
            .DataSource = ds.Tables("DIENTICH")
            .DisplayMember = "M2"
            .ValueMember = "ID"
        End With
        cmbDienTich.SelectedValue = 0
    End Sub
    Private Sub LoadcmbSucChua()
        Dim dt As DataTable = New DataTable("SUCCHUA")
        dt.Columns.Add("ID")
        dt.Columns.Add("SOLUONG")

        dt.Rows.Add(1, "Ít hơn hoặc bằng 5 người")
        dt.Rows.Add(2, "Nhiều hơn 5 người")
        dt.Rows.Add(0, "Tất cả")
        Dim ds As New DataSet("SUCCHUA")
        ds.Tables.Add(dt)
        With cmbSucChua
            .DataSource = ds.Tables("SUCCHUA")
            .DisplayMember = "SOLUONG"
            .ValueMember = "ID"
        End With
        cmbSucChua.SelectedValue = 0
    End Sub
    Private Sub LoadcmbQuan()
        Dim ds As New DataSet()
        ds = connect.LoadQuan("select MAQUAN,TENQUAN from QUAN")
        Dim newrow As DataRow = ds.Tables("QUAN").NewRow()
        newrow("TENQUAN") = "Tất cả"
        newrow("MAQUAN") = 0
        ds.Tables("QUAN").Rows.Add(newrow)
        With cmbQuan
            .DataSource = ds.Tables("QUAN")
            .DisplayMember = "TENQUAN"
            .ValueMember = "MAQUAN"
        End With
        cmbQuan.SelectedValue = 0
    End Sub
    Private Sub btnChon_Click(sender As System.Object, e As System.EventArgs) Handles btnChon.Click
        Dim frm As New frmTimKH()
        frm.setflag(2)
        frm.ShowDialog()
    End Sub
    Private Sub LamMoi()
        dgvKetQua.ClearSelection()
        txtMaKH.Text = ""
        txtMaTin.Text = ""
        txtTieuDe.Text = ""
        txtCMND.Text = ""
        txtTenKH.Text = ""
        txtGia.Text = ""
        txtDonVi.Text = ""
        btnThue.Enabled = False
        btnChon.Enabled = False
    End Sub
    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        LamMoi()
    End Sub

    Private Sub dgvKetQua_SelectionChanged(sender As System.Object, e As System.EventArgs) Handles dgvKetQua.SelectionChanged
        txtMaTin.Text = dgvKetQua.CurrentRow.Cells(0).Value.ToString().Trim()
        txtTieuDe.Text = dgvKetQua.CurrentRow.Cells(1).Value.ToString.Trim()
        txtGia.Text = dgvKetQua.CurrentRow.Cells(12).Value.ToString().Trim()
        txtDonVi.Text = dgvKetQua.CurrentRow.Cells(13).Value.ToString().Trim()
        btnChon.Enabled = True
    End Sub

    Private Sub txtMaKH_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtMaKH.TextChanged
        If (txtMaKH.Text <> "") Then
            btnThue.Enabled = True
        End If
    End Sub

    Private Sub txtTuKhoa_Click(sender As System.Object, e As System.EventArgs) Handles txtTuKhoa.Click
        txtTuKhoa.Text = ""
    End Sub

    Private Sub btnTim_Click(sender As System.Object, e As System.EventArgs) Handles btnTim.Click
        Dim i As Integer
        Dim tuKhoa As String = txtTuKhoa.Text.Trim()
        If (tuKhoa = "Nhập từ khoá vào đây!") Then
            tuKhoa = ""
        End If
        Dim iloai As Integer = Convert.ToInt32(cmbLoai.SelectedValue)
        Dim loai As String
        Select Case iloai
            Case 0
                loai = "'%'"
            Case 1
                loai = "1"
            Case 2
                loai = "2"
            Case 3
                loai = "3"
        End Select
        Dim iQuan As Integer = Convert.ToInt32(cmbQuan.SelectedValue)
        Dim quan As String
        If (iQuan = 0) Then
            quan = "'%'"
        Else
            For i = 1 To cmbQuan.Items.Count() Step 1
                If (iQuan = i) Then
                    quan = i.ToString()
                End If
            Next
        End If
        Dim iSucChua As Integer = Convert.ToInt32(cmbSucChua.SelectedValue)
        Dim sucChua As String
        Select Case iSucChua
            Case 0
                sucChua = ">=0"
            Case 1
                sucChua = "<=5"
            Case 2
                sucChua = ">5"
        End Select
        Dim iDienTich As Integer = Convert.ToInt32(cmbDienTich.SelectedValue)
        Dim dienTich As String
        Select Case iDienTich
            Case 0
                dienTich = ">=0"
            Case 1
                dienTich = "<=5"
            Case 2
                dienTich = ">5"
        End Select
        Dim iGiaTien As Integer = Convert.ToInt32(cmbGia.SelectedValue)
        Dim giaTien As String
        Select Case iGiaTien
            Case 0
                giaTien = ">=0"
            Case 1
                giaTien = "<1000000 and MADV='VND'"
            Case 2
                giaTien = ">=1000000 and GIATIEN <=2000000 and MADV='VND'"
            Case 3
                giaTien = ">=2000000 and GIATIEN <=5000000 and MADV='VND'"
            Case 4
                giaTien = ">5000000 and MADV='VND'"
        End Select
        Dim ganCho As String = cbGanCho.Checked.ToString().Trim()
        Dim coInternet As String = cbCoInternet.Checked.ToString().Trim()
        Dim ganBus As String = cbGanBus.Checked.ToString().Trim()

        Dim sql As String = "select * from TINDANG where TIEUDE like '%" + tuKhoa + "%' and MALP like " + loai + " and MAQUAN like " + quan + " and GIATIEN " + giaTien + " and DIENTICH " + dienTich + " and SUCCHUA " + sucChua + " and DATHUE='False' and BIXOA='False'"
        Dim dt As New DataTable
        dt = connect.GetData(sql)
        dgvKetQua.DataSource = dt
    End Sub
    Private Function TaoMaPhieu() As String
        Dim yy As Integer = DateTime.Today.Year Mod 100
        Dim mm As Integer = DateTime.Today.Month

        Dim ngay As String = yy.ToString() + mm.ToString("00")
        Dim dt As DataTable = connect.GetData("select * from PHIEUTHUE where MAPT like '" + ngay + "%'")
        Dim stt As Integer = dt.Rows.Count() + 1
        Dim str_stt As String = stt.ToString("000")
        Dim maTin As String = ngay + str_stt
        Dim check As Boolean = False
        While (check = False)
            Dim dt2 As DataTable = connect.GetData("select * from PHIEUTHUE where MAPT = '" + maTin + "'")
            If (dt2.Rows.Count() > 0) Then
                stt = stt + 1
                str_stt = stt.ToString("000")
                maTin = ngay + str_stt
                Dim dt3 As DataTable = connect.GetData("select * from PHIEUTHUE where MAPT ='" + maTin + "'")
                If (dt3.Rows.Count() > 0) Then
                    check = False
                Else
                    check = True
                End If
            Else
                check = True
            End If
        End While
        Return maTin
    End Function
    Private Sub btnThue_Click(sender As System.Object, e As System.EventArgs) Handles btnThue.Click
        Dim maPhieuThue As String = TaoMaPhieu()
        Dim maTinDang As String = txtMaTin.Text.Trim()
        Dim maKH As String = txtMaKH.Text.Trim()
        Dim ngay As DateTime = DateTime.Today()
        Dim ngayThue As String = ngay.Year().ToString("0000") + "-" + ngay.Month().ToString("00") + "-" + ngay.Day().ToString("00")
        Dim giaThue As String = txtGia.Text.Trim()
        Dim maDV As String = txtDonVi.Text.Trim()
        Dim biXoa = "False"

        Dim sql As String = "insert into PHIEUTHUE(MAPT,MAKH,NGAYTHUE,MATD,GIATHUE,MADV,BIXOA) values('" + maPhieuThue + "','" + maKH + "','" + ngayThue + "','" + maTinDang + "'," + giaThue + ",'" + maDV + "','" + biXoa + "')"
        Dim sqlupdate As String = "update TINDANG set DATHUE='True' where MATD='" + maTinDang + "'"
        connect.ExecSql(sql)
        connect.ExecSql(sqlupdate)
        Dim frm As New frmKhachHang()
        frm.ganID(maKH)
        MessageBox.Show("Thuê thành công!")
        frm.ShowDialog()
        LamMoi()
    End Sub
End Class
